import requests
import logging
import allure
from utils.error_handler import assert_and_log, log_error

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

class RequestUtils:
    def __init__(self):
        self.session = requests.Session()

    def send_request(self, method, url, **kwargs):
        try:
            with allure.step(f"发送 {method} 请求到 {url}"):
                response = self.session.request(method, url, **kwargs)
                logging.info(f"请求成功: {method} {url}")
                allure.attach(
                    body=str(kwargs),
                    name="请求参数",
                    attachment_type=allure.attachment_type.TEXT
                )
                allure.attach(
                    body=str(response.status_code),
                    name="响应状态码",
                    attachment_type=allure.attachment_type.TEXT
                )
                allure.attach(
                    body=str(response.json()),
                    name="响应体",
                    attachment_type=allure.attachment_type.TEXT
                )
                return response
        except requests.exceptions.RequestException as e:
            logging.error(f"请求失败: {e}")
            error_message = f"请求失败: {e}"
            log_error(error_message)
            return None